﻿using Assets.Scripts.EffectorObject;
using System.Collections;
using UnityEngine;

[RequireComponent(typeof(C_BasicAttribute))]
[RequireComponent(typeof(C_FollowTarget))]
public class C_ThrowSystem : C_Base
{
    CData_ThrowSystem Data
    {
        get
        {
            return GetData<CData_ThrowSystem>();
        }
    }

    C_BasicAttribute basicAttribute;
    C_FollowTarget followTarget;

    private bool startThrow = false;
    private Missile missile;

    string callbackFuncNames = "BeforeThrow,CancleThrow,Throw,AfterThrow";
    public override void OnAddComponent()
    {
        base.OnAddComponent();
        GetComponent<C_BasicAttribute>().OnAddComponent();
        GetComponent<C_FollowTarget>().OnAddComponent();
        componentManager.AddCallbackFuncName(callbackFuncNames);
    }
    public override void OnRemoveComponent()
    {
        base.OnRemoveComponent();
        componentManager.RemoveCallbackFuncName(callbackFuncNames);
    }
    public override void Equip(params Object[] args)
    {
        base.Equip();
    }
    public override void UnEquip(params Object[] args)
    {
        base.UnEquip();
    }
    private void Start()
    {
        basicAttribute = GetComponent<C_BasicAttribute>();
        followTarget = GetComponent<C_FollowTarget>();
        CloneData();
    }

    private void Update()
    {
        if(startThrow && missile != null)
        {
            missile.SetTargetPos(followTarget.GetTargetPoint());
            missile.ShowIndicator();
        }
    }
    public virtual void BeforeThrow()
    {
        if (missile == null)
        {
            missile = gameObject.AddComponent<Missile>();
            missile.Init(Data.missileData, AfterThrow, Data.duration);
            missile.SetTargetPos(followTarget.GetTargetPoint());
            missile.ShowIndicator();
            startThrow = true;
        }
    }
    public virtual void CancleThrow()
    {
        startThrow = false;
        missile.HideIndicator();
        Destroy(missile);
    }
    public virtual void Throw()
    {
        startThrow = false;
        missile.HideIndicator();
        missile.Throw();
    }
    public virtual void AfterThrow()
    {
        
    }
}